package com.attendance.pojo;

import com.attendance.base.BaseEntity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜单
 *
 * @author 鵺丶
 * @date 2021/03/14
 */
@Data
@Component
@EqualsAndHashCode(callSuper = true)
@TableName(value = "t_menu"
//,resultMap = "menuResultMap"
)
public class Menu extends BaseEntity {
    @TableId(type = IdType.AUTO)
    private Long id;
    private Long idx;
    private String title;
    private String url;
    private String icon;
    private Long parentId;
    @TableField(exist = false)
    private List<Menu> children = new ArrayList<>();

    public static List<MenuForSelect> menuToSelectMenu(List<Menu> menus, Boolean checked, Boolean openUrl) {
        List<MenuForSelect> menuForSelects = new ArrayList<>();
        for (Menu menu : menus) {
            MenuForSelect menuForSelect = new MenuForSelect(menu, checked, openUrl);
            menuForSelects.add(menuForSelect);
        }
        return menuForSelects;
    }

    @EqualsAndHashCode(callSuper = true)
    @Data
    public static class MenuForSelect extends Menu {
        private Boolean checked;
        private Boolean open;

        public MenuForSelect(Menu menu, Boolean checked, Boolean openUrl) {
            this.setId(menu.getId());
            this.setIdx(menu.getIdx());
            this.setTitle(menu.getTitle());
            if (openUrl) {
                this.setUrl(menu.getUrl());
            }
            this.setIcon(menu.getIcon());
            this.setParentId(menu.getParentId());
            this.setChildren(menu.getChildren());

            this.setOpen(checked);
        }
    }
}
